home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / titlemgt / paintacc.sx < prev    next >
Encoding:
Text File  |  1996-05-21  |  6.9 KB  |  196 lines  |  [TEXT/ttxt]

  1. --<<<
  2. -- Filename:
  3. --     PAINTACC.SX
  4.  
  5. -- Other Files Required: 
  6. --     GAUGUIN.BMP bitmap image (400 x 300)
  7.  
  8. -- Purpose:
  9. --     Intended to be a simple title which stores bitmaps in a separate library file.
  10. --     and includes an accessory which zooms the bitmap larger and smaller.
  11.  
  12. -- Specialized Classes: ExtendableTitleContainer and ZoomAccessory
  13.  
  14. -- Instructions to User:
  15. --     1. Open this script.  It will create three files:
  16. --                A title file called       PAINTING.SXT
  17. --                A library file called     PAINTING.SXL
  18. --                An accessory file called  PAINTING.SXA
  19. --     2. Quit ScriptX.  Open the newly created title container PAINTING.SXT 
  20. --        from the operating system.  It should open up the title container and 
  21. --        library container, and display the painting.
  22. --       3. Choose "Open Accessory" from the File menu and open PAINTING.SXA.  
  23. --        This should display "To zoom, move the mouse left or right", and allow 
  24. --        you to zoom the picture larger or smaller. Click to remove the 
  25. --        accessory
  26.  
  27. -- Authors:
  28. --     Erik Neumann, Douglas Kramer
  29.  
  30. ------------------------------------------------------------------------------
  31. -- CREATE THE TITLE **********************************************************
  32. ------------------------------------------------------------------------------
  33. -- Create the window
  34. global myWindow := new Window boundary: (new Rect x1: 0 y1: 40 x2: 400 y2: 300)
  35. myWindow.name := "Gauguin"
  36. show myWindow
  37.  
  38. ------------------------------------------------------------------------------
  39. -- Create and append a TwoDShape to the window
  40. global myShape := new TwoDShape
  41. append myWindow myShape
  42.  
  43. ------------------------------------------------------------------------------
  44. -- Define a subclass of TitleContainer
  45. class ExtendableTitleContainer (TitleContainer)
  46. end
  47.  
  48. -- Get the objects from the accessory
  49. method addAccessory self {class ExtendableTitleContainer} accContainer -> (
  50.     local accList := nextMethod self accContainer
  51.  
  52.     -- Add the @text item from the accessory to the frontmost window 
  53.     prepend accContainer.windowHoldingAccessory accList[@text]
  54. )
  55.  
  56. -- Remove the objects from the accessory (Undo what addAccessory did)
  57. method removeAccessory self {class ExtendableTitleContainer} accContainer -> (
  58.     local myText := chooseOne accContainer.windowHoldingAccessory \
  59.                         (v a -> isAKindOf v TextPresenter) 0
  60.     if myText = undefined do exit with myText
  61.     deleteOne accContainer.windowHoldingAccessory myText.target
  62.     nextMethod self accContainer
  63. )
  64.  
  65. -- Create the title container and add the window to it
  66. global tc := new ExtendableTitleContainer dir:theScriptDir path:"painting.sxt" \
  67.     name:"Painting Title"
  68. append tc myWindow
  69.  
  70. -- Define the title's startup action
  71. tc.startupAction := (tc -> 
  72.  
  73.     -- Load all objects in title container
  74.     forEach tc load undefined
  75.     
  76.     -- Get the painting library
  77.     local myLC := chooseOne theOpenContainers \
  78.                     (v a -> v.name = "Painting Library") 0
  79.  
  80.     -- Assign the bitmap to the 2D shape’s target in the frontmost window
  81.     tc.windows[1][1].target := myLC[1]
  82. )
  83.  
  84. ------------------------------------------------------------------------------
  85. -- CREATE THE LIBRARY ********************************************************
  86. ------------------------------------------------------------------------------
  87.  
  88. function getPict fileName -> (
  89.     local myStream := getStream theScriptDir fileName @readable
  90.     local myimage := importMedia theImportExportEngine myStream @image @dib \
  91.         @bitmap colormap:defaultcolormap
  92.     myimage
  93. )
  94.  
  95. global p1 := getPict "GAUGUIN.BMP"
  96.  
  97. ------------------------------------------------------------------------------
  98. -- Create the library, and append the picture to it
  99. -- Note that this library uses the title container tc defined previously
  100. global lc := new LibraryContainer dir:theScriptDir path:"painting.sxl" \
  101.     name:"Painting Library" user:tc
  102. append lc p1
  103.  
  104. -- Close the title container and then the library container
  105. close tc
  106. close lc
  107.  
  108.  
  109. ------------------------------------------------------------------------------
  110. -- CREATE THE ACCESSORY ******************************************************
  111. ------------------------------------------------------------------------------
  112. -- Create an accessory container which must be added by the user
  113. class ZoomAccessory (AccessoryContainer)
  114.     instance variables
  115.         mouseMoveInterest            -- Only reason for these ivs is they
  116.         mouseDownInterest            -- hold onto objects so those objects
  117.         windowHoldingAccessory        -- can later be removed
  118. end
  119.  
  120. -- Remove event interests MouseEvent classes
  121. method removeAccEventInterests self {class ZoomAccessory} interest event -> (
  122.     if (self.mouseMoveInterest != undefined) do
  123.         removeEventInterest self.mouseMoveInterest
  124.     if (self.mouseDownInterest != undefined) do
  125.         removeEventInterest self.mouseDownInterest
  126. )
  127.  
  128. -- This method is needed only to change the number and order of arguments
  129. method callRemoveAccessory self {class ZoomAccessory} interest event -> (
  130.     removeAccessory theTitleContainer self
  131. )
  132.  
  133. -- Method called by addAccessory from the title
  134. method getAccessory self {class ZoomAccessory} -> (
  135.     -- Locate the bitmap in the title
  136.     self.windowHoldingAccessory := theTitleContainer.windows[1]
  137.     local myShape := chooseOne self.windowHoldingAccessory \
  138.         (v a -> isAKindOf v TwoDShape) 0
  139.     local myBitmap := myShape.boundary
  140.  
  141.     -- Scale the image
  142.     function scaleIt theTwoDShape interest event -> (
  143.         if (event.screenCoords.x > 1) do (
  144.             local myScale := 2 * event.screenCoords.x / theTwoDShape.width
  145.             local myMatrix := scale (new TwoDMatrix) myScale myScale
  146.             transform myBitmap myMatrix @mutate
  147.             theTwoDShape.changed := true
  148.             theTwoDShape.x := (theTwoDShape.presentedBy.width \
  149.                 - theTwoDShape.width)/2
  150.             theTwoDShape.y := (theTwoDShape.presentedBy.height \
  151.                 - theTwoDShape.height)/2
  152.         )
  153.     )
  154.     
  155.     -- Set up the mouse move event
  156.     self.mouseMoveInterest := new MouseMoveEvent
  157.     self.mouseMoveInterest.authorData := myShape
  158.     self.mouseMoveInterest.presenter := undefined
  159.     self.mouseMoveInterest.device := new MouseDevice
  160.     self.mouseMoveInterest.eventReceiver := scaleIt
  161.  
  162.     addEventInterest self.mouseMoveInterest
  163.     
  164.     -- Set up the mouse down event
  165.     self.mouseDownInterest := new MouseDownEvent
  166.     self.mouseDownInterest.authorData := self
  167.     self.mouseDownInterest.presenter := undefined
  168.     self.mouseDownInterest.device := new MouseDevice
  169.     self.mouseDownInterest.eventReceiver := callRemoveAccessory
  170.  
  171.     addEventInterest self.mouseDownInterest
  172.  
  173.     -- Add text to the accessory
  174.     local myText := new TextPresenter boundary:(new Rect x2:400 y2:20) \
  175.             target:" To zoom, move mouse left or right. Click to stop."
  176.     setDefaultAttr myText @alignment @center
  177.  
  178.     myText.fill := whitebrush
  179.     
  180.     -- Create the list to be returned by getAccessory
  181.     local accList := new HashTable 
  182.     add accList @text myText
  183.     accList
  184. )
  185.  
  186. -- Remove the event interests when title is closed
  187. method terminate self {class ZoomAccessory} -> (
  188.     removeAccEventInterests self undefined undefined
  189.     nextMethod self
  190. )
  191.  
  192. global ac := new ZoomAccessory dir:theScriptDir path:"painting.sxa" \
  193.     name:"Painting Accessory"
  194. close ac
  195.  
  196.